package com.xoa.service.financialManage.charge.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xoa.dao.financialManage.charge.ChargeSettingMapper;
import com.xoa.dao.financialManage.charge.ChargeSettingTypeMapper;
import com.xoa.model.financialManage.charge.ChargeSetting;
import com.xoa.model.financialManage.charge.ChargeSettingType;
import com.xoa.model.financialManage.charge.ChargeSettingTypeExample;
import com.xoa.service.financialManage.charge.ChargeSettingService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 预警设置 服务实现类
 * </p>
 *
 * @author liujian
 * @since 2020-04-23
 */
@Service
public class ChargeSettingServiceImpl extends ServiceImpl<ChargeSettingMapper, ChargeSetting> implements ChargeSettingService {

    @Autowired
    private ChargeSettingMapper chargeSettingMapper;

    @Autowired
    private ChargeSettingTypeMapper chargeSettingTypeMapper;

    //查询全部
    @Override
    public ToJson findSetingAll(ChargeSetting chargeSetting, Integer page, Integer limit) {
        ToJson toJson=new ToJson(1,"暂无数据");
        Map map=new HashMap();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(true);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            //设置对象。
            map.put("setType",chargeSetting.getSetType());
            List<ChargeSetting> listseting= chargeSettingMapper.findSetingAll(map);
            if (!listseting.isEmpty()){
                toJson.setObj(listseting);
                PageParams pageParams=(PageParams)map.get("pageParams");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //根据ID查询
    @Override
    public ToJson findSetingById(ChargeSetting chargeSetting) {
        ToJson toJson=new ToJson(1,"暂无数据");
        try {
            ChargeSetting chargeSetting1 = chargeSettingMapper.selectByPrimaryKey(chargeSetting.getSettingId());
            if (chargeSetting1!=null){
                //查找按科目设置
                ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                criteria.andSettingIdEqualTo(chargeSetting1.getSettingId());
                chargeSetting1.setTlist(chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample));

                toJson.setObject(chargeSetting1);
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //编辑和新增
    @Override
    public ToJson enidOrInsertSeting(ChargeSetting chargeSetting) {
        ToJson toJson=new ToJson(1,"失败");
        try {
            Date date = new Date();
            int i=0;
            ChargeSettingType[] chargeSettingTypes =null;
            if (!StringUtils.checkNull(chargeSetting.getStrJson())){
                chargeSettingTypes=(ChargeSettingType[])JSONArray.toArray(JSONArray.fromObject(chargeSetting.getStrJson()),ChargeSettingType.class);
            }

            if (chargeSetting.getSettingId()!=null){
                i=chargeSettingMapper.updateByPrimaryKeySelective(chargeSetting);
                //判断中间表是否存在
                if (chargeSettingTypes!=null && chargeSetting.getAlertType().equals("2")){
                    ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                    ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                    criteria.andSettingIdEqualTo(chargeSetting.getSettingId());
                    List<ChargeSettingType> chargeSettingTypes1 = chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample);
                    if (chargeSettingTypes1.size()>0){
                        for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                            ChargeSettingTypeExample chargeSettingTypeExample2=new ChargeSettingTypeExample();
                            ChargeSettingTypeExample.Criteria criteria2 = chargeSettingTypeExample2.createCriteria();
                            criteria2.andSettingIdEqualTo(chargeSetting.getSettingId());
                            criteria2.andChargeTypeIdEqualTo(chargeSettingType.getChargeTypeId());
                            chargeSettingTypeMapper.updateByExampleSelective(chargeSettingType,chargeSettingTypeExample2);
                        }
                    }else {
                        for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                            chargeSettingType.setSettingId(chargeSetting.getSettingId());
                            chargeSettingType.setCreatedTime(date);
                            chargeSettingTypeMapper.insertSelective(chargeSettingType);
                        }
                    }
                }
            }else {
                //创建时间
                chargeSetting.setCreatedTime(date);

                //新增的时候判断新增对象
                if (!StringUtils.checkNull(chargeSetting.getSetType()) && chargeSetting.getSetType().equals("4")){
                    //角色,判断是这个角色的用户是否存在，如果不存在取出这个角色的所有用户
                    Map map=new HashMap();
                    map.put("setType","3");
                    List<ChargeSetting> setingAll = chargeSettingMapper.findSetingAll(map);
                    if (setingAll.size()>0){
                        for (ChargeSetting chargeSetting1 : setingAll) {
                            //角色
                            chargeSetting.setSettingId(chargeSetting1.getSettingId());
                            chargeSetting.setSetType(chargeSetting1.getSetType());
                            i=chargeSettingMapper.updateByPrimaryKeySelective(chargeSetting);

                            //判断中间表是否存在
                            if (chargeSettingTypes!=null && chargeSetting.getAlertType().equals("2")){
                                ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                                ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                                criteria.andSettingIdEqualTo(chargeSetting1.getSettingId());
                                List<ChargeSettingType> chargeSettingTypes1 = chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample);
                                if (chargeSettingTypes1.size()>0){
                                    for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                        ChargeSettingTypeExample chargeSettingTypeExample2=new ChargeSettingTypeExample();
                                        ChargeSettingTypeExample.Criteria criteria2 = chargeSettingTypeExample2.createCriteria();
                                        criteria2.andSettingIdEqualTo(chargeSetting1.getSettingId());
                                        criteria2.andChargeTypeIdEqualTo(chargeSettingType.getChargeTypeId());
                                        chargeSettingTypeMapper.updateByExampleSelective(chargeSettingType,chargeSettingTypeExample2);
                                    }
                                }else {
                                    for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                        chargeSettingType.setSettingId(chargeSetting.getSettingId());
                                        chargeSettingType.setCreatedTime(date);
                                        chargeSettingTypeMapper.insertSelective(chargeSettingType);
                                    }
                                }
                            }
                        }
                    }else {
                        //查到这个角色下所有的用户进行新增
                        List<Map<String,Object>> strings = chargeSettingMapper.userByuserPriv(chargeSetting.getPrivId());
                        for (Map<String,Object> user:strings) {
                            String user_id =(String) user.get("USER_ID");
                            String user_name =(String) user.get("USER_NAME");
                            chargeSetting.setSettingId(null);
                            chargeSetting.setSetType("3");
                            chargeSetting.setUserId(user_id);
                            chargeSetting.setUserName(user_name);
                            i = chargeSettingMapper.insertSelective(chargeSetting);

                            //判断中间表是否存在,并添加
                            if (chargeSettingTypes!=null && chargeSetting.getAlertType().equals("2")){
                                ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                                ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                                criteria.andSettingIdEqualTo(chargeSetting.getSettingId());
                                List<ChargeSettingType> chargeSettingTypes1 = chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample);
                                if (chargeSettingTypes1.size()>0){
                                    for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                        ChargeSettingTypeExample chargeSettingTypeExample2=new ChargeSettingTypeExample();
                                        ChargeSettingTypeExample.Criteria criteria2 = chargeSettingTypeExample2.createCriteria();
                                        criteria2.andSettingIdEqualTo(chargeSetting.getSettingId());
                                        criteria2.andChargeTypeIdEqualTo(chargeSettingType.getChargeTypeId());
                                        chargeSettingTypeMapper.updateByExampleSelective(chargeSettingType,chargeSettingTypeExample2);
                                    }
                                }else {
                                    for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                        chargeSettingType.setSettingId(chargeSetting.getSettingId());
                                        chargeSettingType.setCreatedTime(date);
                                        chargeSettingTypeMapper.insertSelective(chargeSettingType);
                                    }
                                }
                            }

                        }
                    }

                }else if (!StringUtils.checkNull(chargeSetting.getSetType()) && (chargeSetting.getSetType().equals("3") || chargeSetting.getSetType().equals("2")) ){
                    //用户 和部门
                    Map map=new HashMap();
                    map.put("setType",chargeSetting.getSetType());
                    if (chargeSetting.getSetType().equals("3")){
                        map.put("userId",chargeSetting.getUserId());
                    }else if (chargeSetting.getSetType().equals("2")){
                        map.put("deptId",chargeSetting.getDeptId());
                    }

                    //查询是否存在
                    List<ChargeSetting> setingAll = chargeSettingMapper.findSetingAll(map);
                    if (setingAll.size()>0){
                        ChargeSetting chargeSetting1 = setingAll.get(0);
                        chargeSetting.setSettingId(chargeSetting1.getSettingId());
                        i=chargeSettingMapper.updateByPrimaryKeySelective(chargeSetting);

                        //判断是否是按科目设置
                        //判断中间表是否存在
                        if (chargeSettingTypes!=null && chargeSetting.getAlertType().equals("2")){
                            ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                            ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                            criteria.andSettingIdEqualTo(chargeSetting1.getSettingId());
                            List<ChargeSettingType> chargeSettingTypes1 = chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample);
                            if (chargeSettingTypes1.size()>0){
                                for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                    ChargeSettingTypeExample chargeSettingTypeExample2=new ChargeSettingTypeExample();
                                    ChargeSettingTypeExample.Criteria criteria2 = chargeSettingTypeExample2.createCriteria();
                                    criteria2.andSettingIdEqualTo(chargeSetting1.getSettingId());
                                    criteria2.andChargeTypeIdEqualTo(chargeSettingType.getChargeTypeId());
                                    chargeSettingTypeMapper.updateByExampleSelective(chargeSettingType,chargeSettingTypeExample2);
                                }
                            }else {
                                for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                    chargeSettingType.setSettingId(chargeSetting.getSettingId());
                                    chargeSettingType.setCreatedTime(date);
                                    chargeSettingTypeMapper.insertSelective(chargeSettingType);
                                }
                            }
                        }
                    }else {
                        i=chargeSettingMapper.insertSelective(chargeSetting);
                        //2按科目设置
                        if (chargeSettingTypes!=null && chargeSetting.getAlertType().equals("2")){
                            for (ChargeSettingType chargeSettingType:chargeSettingTypes) {
                                chargeSettingType.setSettingId(chargeSetting.getSettingId());
                                chargeSettingType.setCreatedTime(date);
                                chargeSettingTypeMapper.insertSelective(chargeSettingType);
                            }
                        }
                    }
                }
            }
            if (i>0){
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    //删除
    @Override
    public ToJson delSeting(ChargeSetting chargeSetting) {
        ToJson toJson=new ToJson(1,"失败");
        try {
            String[] split = chargeSetting.getSettingIds().split(",");
            int i=chargeSettingMapper.deleteByIds(Arrays.asList(split));
            if (i>0){
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
